﻿var $url = new Url();
var dict = new Dict();//字典信息
var $Id = $url.GetQueryString("orderId");

//绑定对应数据
$(function () {
    if (!$Id) {//添加新订单
        $(".lblUserName").css("display", "none");
        $(".txtUserName").focus(function () { $(".btnShow").click() });
    }
    else {//编辑订单 
        $(".txtUserName,.btnShow,.validateCode,#btnSendCaptcha").css("display", "none");
        $(".txtTel,.txtIDCard").attr("disabled", "disabled");
        var userId = $(".hdUserId").val();
        //当前订单理财规模
        $(".OrderAmount").text($(".txtAmount").val() + "万元");
        var $OrderId = $Id;
        if (!$OrderId) {
            $OrderId = $(".NewOrderId").val();
        }
    }
})

//收益回款账户 设置 
$(".Profit").click(function () {
    var userId = $(".hdUserId").val();
    var $OrderId = $(".NewOrderId").val();
    //保存状态下
    if (!$Id && $OrderId) {
        $.ajax({
            url: "../Handler/FinancialOrder.ashx",
            type: "post",
            data: { type: 32, UserId: userId },
            dataType: 'json',
            cache: false,
            success: function (result) {
                if (result.Status) {
                    var str = '<option value="-1">请选择</option>';
                    str += '<option value="' + $OrderId + '">' + $OrderId + '</option>';
                    $(result.Data).each(function (i, e) {
                        str += '<option value="' + e.OrderId + '">' + e.OrderId + '</option>';
                    });
                    str += '<option value="-2">其他</option>';
                    $("#ddlProfitOrder").html(str);
                }
                else {
                    var str = '<option value="-1">请选择</option>';
                    str += '<option value="' + $OrderId + '">' + $OrderId + '</option>';
                    str += '<option value="-2">其他</option>';
                    $("#ddlProfitOrder").html(str);
                }
            }
        })
    }
        //编辑状态
    else {
        $OrderId = $(".OrderId").val();

        //alert("编辑状态");
        //alert($OrderId);
        $.ajax({
            url: "../Handler/FinancialOrder.ashx",
            type: "post",
            data: { type: 32, UserId: userId, OrderId: $OrderId },
            dataType: 'json',
            cache: false,
            success: function (result) {
                if (result.Status) {
                    var str = '<option value="-1">请选择</option>';
                    $(result.Data).each(function (i, e) {
                        str += '<option value="' + e.OrderId + '">' + e.OrderId + '</option>';
                    });
                    str += '<option value="-2">其他</option>';
                    $("#ddlProfitOrder").html(str);

                    //编辑时绑定
                    $.ajax({
                        url: "../Handler/FinancialOrder.ashx",
                        type: "post",
                        data: { OrderId: $OrderId, type: 30 },
                        dataType: 'json',
                        cache: false,
                        success: function (result) {
                            if (result.Status) {

                                //如果是保存的是新增的银行卡信息
                                if (result.Data.BankOrderId == "-2" || !result.Data.BankOrderId) {
                                    $("#BackBankInfo").css("display", "none");
                                    $("#OtherBackBankInfo").css("display", "block");
                                    if (result.Data.BackAccountName) {
                                        $(".txtBackAccountName").val(result.Data.BackAccountName);
                                    }
                                    if (result.Data.BackAccountNum) {
                                        $(".txtBackAccountNum").val(result.Data.BackAccountNum);
                                    }
                                    if (result.Data.BackBankName) {
                                        $(".txtBackBankName").val(result.Data.BackBankName);
                                    }
                                    if (result.Data.BackBranchBankName) {
                                        $(".txtBackBranchBankName").val(result.Data.BackBranchBankName);
                                    }
                                    $("#ddlProfitOrder option[value= -2 ]").attr('selected', 'selected');
                                }
                                    //如果保存的是原有订单中的银行卡信息
                                else {
                                    $("#BackBankInfo").css("display", "block");
                                    $("#OtherBackBankInfo").css("display", "none");
                                    if (result.Data.BackAccountName) {
                                        $(".spanBackAccountName").text(result.Data.BackAccountName);
                                    }
                                    if (result.Data.BackAccountNum) {
                                        $(".spanBackAccountNum").text(result.Data.BackAccountNum);
                                    }
                                    if (result.Data.BackBankName) {
                                        $(".spanBackBankName").text(result.Data.BackBankName);
                                    }
                                    if (result.Data.BackBranchBankName) {
                                        $(".spanBackBranchBankName").text(result.Data.BackBranchBankName);
                                    }
                                    $("#ddlProfitOrder option[value=" + result.Data.BankOrderId + "]").attr('selected', 'selected');
                                }
                            }
                            else {
                                //alert(result.Message);
                            }
                        }
                    });
                    //$("#ddlProfitOrder option[value=" + $OrderId + "]").attr('selected', 'selected');
                } else {
                    var str = '<option value="-1">请选择</option>';
                    str += '<option value="' + $OrderId + '">' + $OrderId + '</option>';
                    str += '<option value="-2">其他</option>';
                    $("#ddlProfitOrder").html(str);
                }

            }
        })
    }


});

//收益回款账户下拉列表事件
$("#ddlProfitOrder").on("change", function () {
    var orderId = $(this).val();
    if (orderId > 0) {
        $("#BackBankInfo").css("display", "block");
        $("#OtherBackBankInfo").css("display", "none");
        $.ajax({
            url: "../Handler/FinancialOrder.ashx",
            type: "post",
            data: { Id: orderId, type: 4 },
            dataType: 'json',
            cache: false,
            success: function (result) {
                if (result.Status) {
                    var $IsSameBank = result.Data.Order.IsSameBank;
                    if ($IsSameBank) {
                        if (result.Data.Order.RecAccName) {
                            $(".spanBackAccountName").text(result.Data.Order.RecAccName);
                        }
                        if (result.Data.Order.RecAccNum) {
                            $(".spanBackAccountNum").text(result.Data.Order.RecAccNum);
                        }
                        if (result.Data.Order.PayBank) {
                            $(".spanBackBankName").text(result.Data.Order.PayBankName);
                        }
                        if (result.Data.Order.PayBankName) {
                            $(".spanBackBranchBankName").text(result.Data.Order.DepositBank);
                        }
                    }
                    else {
                        if (result.Data.Order.TransAccName) {
                            $(".spanBackAccountName").text(result.Data.Order.TransAccName);
                        }
                        if (result.Data.Order.TransAccNum) {
                            $(".spanBackAccountNum").text(result.Data.Order.TransAccNum);
                        }
                        if (result.Data.Order.TransferBackBankName) {
                            $(".spanBackBankName").text(result.Data.Order.TransferBackBankName);
                        }
                        if (result.Data.Order.TransOpenBank) {
                            $(".spanBackBranchBankName").text(result.Data.Order.TransOpenBank);
                        }
                    }
                }
                else {
                    alert(result.Message);
                }

            }
        });

    }
    else {
        if (orderId == -2) {
            $("#BackBankInfo").css("display", "none");
            $("#OtherBackBankInfo").css("display", "block");
        }
        else if (orderId == -1) {
            $("#BackBankInfo").css("display", "none");
            $("#OtherBackBankInfo").css("display", "none");
        }
    }
});

//保存设置中的信息
$(".save_Profit").bind("click", function () {
    var errorNode = null;
    var validate = new Validate();
    $(".modal-body .txtFinaOrderId ").each(function (index, item) {
        var value = $(this).val();
        if (value == "" || value == -1) {
            $name = $(this).attr("data-name");
            errorNode = $(this);
            $(this).next("span").text($name + "不能为空！");
            $(this).focus();
            return false;
        }
    });
    if (errorNode) {
        return false;
    }
    var $formData = "";
    var $isChangeBank = $("#ddlProfitOrder").val();
    var $OrderId = $(".NewOrderId").val();

    //根据下拉列表判断是直接取订单中回款账户和银行，还是更换回款账户和银行
    if ($isChangeBank > 0) {
        $formData = serializer.SerializeDataName($("#BackBankInfo")[0]);
        $formData.BankOrderId = $isChangeBank;
        $formData.IsOriginalAccount = 0;
        if (!$Id && $OrderId) {
            $formData.FinaOrderID = $(".NewOrderId").val();
        }
        else {
            $formData.FinaOrderID = $Id;
        }
    }
    else if ($isChangeBank == -2) {
        $("#OtherBackBankInfo .text").each(function () {
            if (!$(this).val() || $(this).val() == -1) {
                errorNode = $(this);
                $name = $(this).attr("data-name");
                $(this).next("span").text($name + "不能为空！");
                $(this).focus();
                return false;
            }
        });
        var validate = new Validate();
        if (!validate.CheckNumber($(".txtBackAccountNum").val())) {
            $(".txtBackAccountNum").focus();
            $(".txtBackAccountNum").next("span").text("请输入正确的数字格式 .例如：100"); //判断只能输入 大于0的整数或小数 正则表达式
            return;
        }
        $formData = serializer.Serialize($("#OtherBackBankInfo")[0]);
        $formData.IsOriginalAccount = 1;
        if (!$Id && $OrderId) {
            $formData.FinaOrderID = $(".NewOrderId").val();
        }
        else {
            $formData.FinaOrderID = $Id;
        }
    }
    if (!$Id && $OrderId) {//保存
        $formData.type = 28;
    }
    else {//编辑
        $formData.type = 29;
    }
    $.ajax({
        url: "../Handler/FinancialOrder.ashx",
        type: "post",
        data: $formData,
        dataType: 'json',
        cache: false,
        success: function (result) {
            if (result.Status) {
                $(".OrderId").val(result.Data);
                alert(result.Message);
            }
            else {
                alert(result.Message);
            }
        }
    });
})

//可绑定年度产品订单详情 按钮
$(".butProductshow").bind("click", function () {
    $(".amountInfo").empty();
    $(".txtProductName").val();
    var userId = $(".hdUserId").val();
    var $OrderId = $(".txtOrderId").val();
    var $ProductName = $(".txtProductName").val();
    //跳点订单详情
    $.ajax({
        url: "../Handler/FinancialOrder.ashx",
        type: "post",
        data: { OrderId: $OrderId, ProductTypeId: $ProductName, "UserId": userId, type: 27, _: Math.random() },
        dataType: 'json',
        cache: false,
        success: function (result) {
            if (result.Status) {
                $(result.Data).each(function (index, item) {
                    $("#ProductTemplete").tmpl(item).appendTo($(".amountInfo"));
                });
            }
            else {
            }
        }
    });
    funSumAmount();
})

//是否全选年度产品信息
$("#chkAmountAll").click(function () {
    var $AmounttChecked = $(this).is(":checked");
    if ($AmounttChecked) {
        $("[name=ckeckboxOrderId]:checkbox").prop("checked", true);
    }
    else {
        $("[name=ckeckboxOrderId]:checkbox").prop("checked", false);
    }
    funSumAmount();
});
//时间字符串转换成时间格式
function data_string(str) {
    var d = eval('new ' + str.substr(1, str.length - 2));
    var ar_date = [d.getFullYear(), d.getMonth() + 1, d.getDate()];
    for (var i = 0; i < ar_date.length; i++) ar_date[i] = dFormat(ar_date[i]);
    return ar_date.join('-');
    function dFormat(i) { return i < 10 ? "0" + i.toString() : i; }
}
//可绑定年度产品订单-理财规模合计
$(".btnSumAmount").click(function () {
    var sumAmount = funSumAmount();
    if (sumAmount < 100) { alert("当前理财规模小于100万元，请选择关联跳点订单。"); return; }
    var strOrderIds = "";
    var userId = $(".hdUserId").val();
    $(".amountInfo [name=ckeckboxOrderId]:checkbox").each(function (i) {
        if ($(this).is(":checked")) {
            var orderId = $(this).parent().parent().children(".orderId").text();
            strOrderIds += orderId + ",";
        }
    });
    strOrderIds = strOrderIds.substring(0, strOrderIds.length - 1);
    var $OrderId = $(".OrderId").val();
    if (!$OrderId) {
        $OrderId = $(".NewOrderId").val();
    }
    //修改跳点收益
    $.ajax({
        url: "../Handler/FinancialOrder.ashx",
        type: "post",
        data: { "type": 33, "OrderIds": strOrderIds, "UserId": userId, "OrderId": $OrderId },
        dataType: 'json',
        cache: false,
        success: function (result) {
            if (result.Status) {
                //所关联订单重叠区间 
                $.ajax({
                    url: "../Handler/FinancialOrder.ashx",
                    type: "post",
                    data: { "type": 31, "UserId": userId, "OrderId": $OrderId },
                    dataType: 'json',
                    cache: false,
                    success: function (result) {
                        var sumAmount = result.Data.SumAmount;
                        if (result.Data.AlreadyRelatedAmount == 0) { $("#profitGroup").css('display', 'none'); }
                        else { $("#profitGroup").css('display', 'block'); }
                        ////可绑定年度产品订单(1..)
                        $("#spanbutProductshow").text(result.Data.OrderIds);
                        //所关联订单跳点基数
                        $("#spanRelatedSumJumpProfit").text(result.Data.AlreadyRelatedAmount == 0 ? "" : result.Data.AlreadyRelatedAmount + "万元");
                        //所关联订单重叠区间
                        $("#spanRelatedOrderRange").text(result.Data.RelatedOrderRange);
                    }
                });
            }
            else {
            }
        }
    });

});
//计算所关联订单跳点基数
function funSumAmount() {
    var $OrderId = $Id;
    if (!$OrderId) {
        $OrderId = $(".NewOrderId").val();
    }
    var sumAmount = "";
    var userId = $(".hdUserId").val();
    $.ajax({
        url: "../Handler/FinancialOrder.ashx",
        type: "post",
        data: { "type": 35, "UserId": userId, "OrderId": $OrderId },
        dataType: 'json',
        async: false,
        cache: false,
        success: function (result) {
            if (result.Status) {
                sumAmount = result.Data;
                $(".amountInfo [name=ckeckboxOrderId]:checkbox").each(function (i) {
                    if ($(this).is(":checked")) {
                        var amount = $(this).parent().parent().children(".amount").text();
                        sumAmount += parseFloat(amount);
                    }
                });
                //sumAmount = parseFloat(sumAmount) + parseFloat($(".txtAmount").val())
                if (parseFloat(sumAmount) < 100) {
                    strAmountNote = "";
                }
                else if (parseFloat(sumAmount) >= 100 && parseFloat(sumAmount) < 300) {
                    strAmountNote = "提示：您可以享受1%的跳点收益。";
                }
                else if (parseFloat(sumAmount) >= 300) {
                    strAmountNote = "提示：您可以享受2%的跳点收益。";
                }
                $("#spanAmountNote").text(strAmountNote);//提示
                $("#spanSumAmount").text(sumAmount + "万元");//合计
            }
            else {
            }
        }
    });
    return sumAmount;
}